//
//  BJBaseViewController.m
//  
//
//  Created by lishen on 2023/3/1.
//

#import "BJBaseViewController.h"

@interface BJBaseViewController ()
@property(assign,nonatomic)BOOL isPresent;
@property (nonatomic)UIView *naviBackBtnView;
@property (nonatomic,strong)UIImageView *pageBroundImage;
@end

@implementation BJBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES];
    self.view.backgroundColor = [UIColor clearColor];
    [self initCustomNavi];
    self.customNaviBar.hidden = YES;
    if (self.navigationController.viewControllers.count > 1){
        self.customNaviBar.hidden = NO;;
    }
    [self whetherPresentEnter];
}



- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.view bringSubviewToFront:self.customNaviBar];

}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

}
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];

}


- (void)initCustomNavi{

    [self.view insertSubview:self.pageBroundImage atIndex:0];
    self.pageBroundImage.makeCons(^{
        make.edge.constants(0);
    });

    self.view.addChild(self.customNaviBar);
    self.customNaviBar.addChild(self.naviContentView);
    self.naviContentView.addChild(self.naviBackBtnView);
    self.naviBackBtnView.addChild(self.naviBackBtn);
    self.naviContentView.addChild(self.naviTitleLb);
    self.naviContentView.addChild(self.rightBtn);
    self.customNaviBar.makeCons(^{
        make.leading.top.trailing.constants(0);
        make.height.constants(44+49);
    });
    
    self.naviContentView.makeCons(^{
        make.leading.bottom.trailing.constants(0);
        make.height.constants(44);
    });
    self.naviBackBtnView.makeCons(^{
        make.leading.constants(10);
        make.width.height.constants(44);
        make.centerY.constants(0);
    });

    self.naviBackBtn.makeCons(^{
        make.width.constants(24);
        make.height.constants(24);
        make.center.view(self.naviBackBtnView);
    });
    
    self.naviTitleLb.makeCons(^{
        make.center.constants(0);
    });
    
    self.rightBtn.makeCons(^{
        make.trailing.constants(-12);
        make.width.height.constants(24);
        make.centerY.constants(0);
    });
    
}

- (UIImageView *)pageBroundImage{
    if (_pageBroundImage == nil) {
        _pageBroundImage = ImageView.img([UIImage imageNamed:@"icon_page_bg"]);
        _pageBroundImage.contentMode = UIViewContentModeScaleAspectFill;
        _pageBroundImage.userInteractionEnabled = YES;
    }
    return _pageBroundImage;
}

- (UIView *)customNaviBar{
    if (_customNaviBar == nil) {
        _customNaviBar = [[UIView alloc] init];
    };
    return _customNaviBar;
}


- (UIView *)naviContentView{
    if (_naviContentView == nil) {
        _naviContentView = View;
    }
    return _naviContentView;
}

- (UILabel *)naviTitleLb{
    if (_naviTitleLb == nil) {
        _naviTitleLb = Label;
    }
    _naviTitleLb.font = [UIFont fontWithName:@"Silkscreen"size:20];
    _naviTitleLb.textColor = [UIColor colorWithHexString:@"#000000"];
    return _naviTitleLb;
}

- (UIImageView *)naviBackBtn{
    if (_naviBackBtn == nil) {
        _naviBackBtn = ImageView.img([UIImage imageNamed:@"ic_back"]);
    }
    return _naviBackBtn;
}

- (UIView *)naviBackBtnView{
    if (_naviBackBtnView == nil) {
        _naviBackBtnView = View;
    }
    _naviBackBtnView.onClick(^{
        !self.isPresent ? [self.navigationController popViewControllerAnimated:YES]:[self dismissViewControllerAnimated:YES completion:nil];

    });

    return _naviBackBtnView;
}


-(void)whetherPresentEnter{
    BOOL isPresent = NO;
    NSArray *viewcontrollers=self.navigationController.viewControllers;
    if (viewcontrollers.count>1) {
        //push方式
        if ([viewcontrollers objectAtIndex:viewcontrollers.count-1]==self)isPresent = NO;
    }else{
        //present方式
        isPresent = YES;
    }
    self.isPresent = isPresent;
}

- (UIButton *)rightBtn{
    if (_rightBtn == nil) {
        _rightBtn = Button;
    }
    [_rightBtn setImage:[UIImage imageNamed:@"ic_open"] forState:UIControlStateNormal];
    _rightBtn.hidden = YES;
    return _rightBtn;
}


- (void)setNaviTitle:(NSString *)naviTitle{
    _naviTitleLb.text = naviTitle;
    _naviTitle = naviTitle;
}

- (void)setNaviTitleColor:(UIColor *)naviTitleColor{
    _naviTitleLb.textColor = naviTitleColor;
    _naviTitleColor = naviTitleColor;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    if (@available(iOS 13.0, *)) {
        return UIStatusBarStyleDarkContent;
    } else {
        return [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    }
}




@end
